<?

function _Sitemap($parent = 0, $path = '', $lang = LANG_DEF) {

	$res = array();

//	extract records that:
//	1. have this parent
//	2. are visible in Sitemap
//	3. are accessible by current user, i.e.
//		PT_ISUSER bit is 0 for ANONYMOUS, 0 or 1 for USER

	if ($pages = dataset('db_list', array(
						'table' => tb('struct'),
						'index' => 'id',
						'where' => "(parent=$parent) AND
									(".dbAndL('tag', PT_MAPABLE).") AND 
									( (".dbAndI('tag', PT_ISUSER).") OR ".(user_info('priv',0) & PT_ISUSER)."!=0)") )) {

		foreach($pages->items as $id => $pg) {
		
			// if non-def lang or empty page name - extract page name from table 'list'
			if ( ($lang != LANG_DEF) || !$pg['name'] ) 
				if ($nm = dataset('db_item', tb('list'), "(id_list=".L_PAGENAMES.") AND (id_index=$id) AND (assoc='$lang')"))
					$pages->items[$id]['name'] = $nm->data['text'];
			

			// check page for hook 'map' to determine it's URL
			if ($code = code('map', $pg['code'])) {	
				if (is_file("_code/_{$code}_map.php")) {
					if ($p = include "_code/_{$code}_map.php") {				// outer code executed
						if (is_string($p)) {								// and returned string - a part of path
							$pages->items[$id]['url'] = $path.$p.'/';
						} 													// !!! or it returned something else - need 2b checked
					}														// !!! or it returned nothing - what can it mean?
				}
			} else {
				$p = $pg['keyword'];										// no support for multiple keywords in one record
																			// due to single Page Name record per Page Id
				$pages->items[$id]['url'] = ($path) ? $path.$p.'/' : $p;
				
			}
			
			$temp = array(	'url' => $pages->items[$id]['url'], 
							'name' => $pages->items[$id]['name']);

			if ($l = _Sitemap($id, $pages->items[$id]['url'], $lang)) 	// if found siblings - link them to 'data'
				$temp['data'] = $l;
				
			if ($pg['kind'] == PK_REDIRECT) 								// if not real page but redirect - show siblings
				$temp = $temp['data'][0];									// usually redirect have ONE sibling	

			$res[] = $temp;													// another brick in the wall
		}

	}
	
	return (sizeof($res)) ? $res : false;
}

?>